<!--  -->
<template>
  <div>
    <div class="goods">
      <!--左侧商品类别列表-->
      <div class="menu-wrapper" ref="menuWrapper">
        <ul>
          <li
            class="menu-item"
            v-for="(lei,index) in category"
            :key="index"
            @click="clickMenuItem(index)"
            :class="{current:index===currentIndex}"
          >
            <span class="text bottom-border-1px">
              {{lei.cname}}
            </span>
          </li>
        </ul>
      </div>
      <!--右侧商品列表-->
      <div class="foods-wrapper">
        <ul ref="foodsUI">
          <li class="food-list-hook" v-for="(lei,index) in shop_goods" :key="index">
            <h1 class="title">{{lei.leiming}}</h1>
            <!-- 类别 -->
            <ul>
              <li
                class="food-item bottom-border-1px"
                v-for="(food,index) in lei.foods"
                :key="index"
              >
                <div class="icon">
                  <img width="57" height="57"
                  :src="food.goods_touxiang">
                </div>
                <div class="content">
                  <h2 class="name">{{food.gname}}</h2>
                  <p class="desc"></p>
                  <!--描述-->
                  <div class="extra">
                    <span class="count">月售{{food.yueshou}}</span>
                    <span>好评率100%</span>
                  </div>
                  <div class="price">
                    <span class="now">￥{{food.price}}</span>
                  </div>
                  <div class="cartcontrol-wrapper">
                    <!--将当前食物对象传递给子组件-->
                    <CartControl :food="food"></CartControl>
                  </div>
                </div>
              </li>
            </ul>
          </li>
        </ul>
      </div>
      <!--添加购物车组件-->
      <ShopCart></ShopCart>
    </div>
  </div>
</template>

<script>
//读取商家商品信息
import { mapState } from "vuex";
//引入滚动条组件
import BScroll from "better-scroll";
//引入 添加到购物车 组件
import CartControl from "../../../components/CartControl/CartControl.vue";
//引入 购物车组件
import ShopCart from "../../../components/ShopCart/ShopCart.vue";
export default {
  data() {
    return {
      tops: [], //右侧每个类别的高度
      scrollY: 0 //右侧滑动的顶部位置
    };
  },
  mounted() {
    //异步请求
    let sid = this.$route.query.sid; //获取传过来的sid
    this.$store.state.sid = sid;
    let leiming = [];
    this.$store.dispatch("getShopCategory").then(() => {
      this.$nextTick(() => {
        const lei = this.$store.state.category;
        for (let i = 0; i < lei.length; i++) {
          leiming[i] = lei[i].cname;
        }
        this.$store.dispatch("getShopfoods").then(() => {
          this.$nextTick(() => {
            const foods = this.$store.state.shopgoods;
            const foods1 = foods.slice(0, 5);
            const foods2 = foods.slice(5, 10);
            const foods3 = foods.slice(10, 15);
            const foods4 = foods.slice(15, 20);
            const foods5 = foods.slice(20, 25);
            const foods6 = foods.slice(25, 30);
            const foodsdx1 = {
              leiming: leiming[0],
              foods: foods1
            };
            const foodsdx2 = {
              leiming: leiming[1],
              foods: foods2
            };
            const foodsdx3 = {
              leiming: leiming[2],
              foods: foods3
            };
            const foodsdx4 = {
              leiming: leiming[3],
              foods: foods4
            };
            const foodsdx5 = {
              leiming: leiming[4],
              foods: foods5
            };
            const foodsdx6 = {
              leiming: leiming[5],
              foods: foods6
            };
            const shopfoods = [
              foodsdx1,
              foodsdx2,
              foodsdx3,
              foodsdx4,
              foodsdx5,
              foodsdx6
            ];
            this.$store.state.shop_goods = shopfoods;
            console.log(this.$store.state.shop_goods);
            this._initScroll(); //等待上面元素DOM更新结束
            this._initTops();
          });
        });
      });
    });
    this.$store.dispatch("getShop");
  },

  components: {
    CartControl,
    ShopCart
  },

  computed: {
    ...mapState(["category"]), //读取某商家商品类别信息
    ...mapState(["shop_goods"]), //读取某商家6类商品信息
    //添加计算属性;依据tops数组与scrollY值判断返回下标
    currentIndex() {
      //得到参与计算的两个数值 scrollY tops
      //const scrollY=this.scrollY;
      //const tops=this.tops;
      const { scrollY, tops } = this;
      //判断scrollY范围
      for (var i = 0; i < tops.length; i++) {
        if (scrollY > tops[i] && scrollY < tops[i + 1]) {
          return i;
        }
      }
      // const index=tops.findIndex((top,i)=>{
      //   return scrollY > top && scrollY <tops[i+1]
      // });
      // return index;
    }
  },

  methods: {
    //点击左侧菜单,右侧商品列表滑动到适当位置
    clickMenuItem(index) {
      //依据左侧元素下标查找tops数组中对应元素y值
      const scrollY = this.tops[index];
      //在data添加scrollY
      this.scrollY = scrollY;
      //将当前y保存scrtollY

      //右侧滑块scrollTo跳转指定位置
      this.foodsScroll.scrollTo(0, -scrollY, 300);
    },
    //当前组件创建多个对象和方法,初始值单独写一个方法
    _initScroll() {
      //创建better-scroll对象 绑定左侧菜单
      new BScroll(".menu-wrapper", {
        click: true //左侧菜单支持点击事件右侧联动
      });
      //创建better-scroll对象 绑定右侧菜单
      this.foodsScroll = new BScroll(".foods-wrapper", {
        probeType: 2, //滑动结束时出发
        click: true
      });
      //为右侧better-scroll对象绑定事件 scroll
      this.foodsScroll.on("scroll", ({ x, y }) => {
        //获取右侧y位置保存
        this.scrollY = Math.abs(y);
        console.log(this.scrollY);
      });
      //为右侧better-scroll对象绑定事件 scrollEnd
      this.foodsScroll.on("scrollEnd", ({ x, y }) => {
        this.scrollY = Math.abs(y);
        console.log(this.scrollY);
      });
    },
    //计算所有商品类别元素高度
    _initTops() {
      //创建数组 tops
      const tops = [];
      //创建变量top=0 优惠li顶端位置 x,y=0
      let top = 0;
      //将top添加到数组
      tops.push(top);
      //查找右侧所有li元素
      const lis = this.$refs.foodsUI.getElementsByClassName("food-list-hook");
      //创建循环遍历所有li获取高度累加保存
      Array.prototype.slice.call(lis).forEach(li => {
        //Array.prototype.slice.call(lis),将类数组lis转为数组
        top += li.clientHeight;
        tops.push(top);
      });
      //将数组tops保存data
      this.tops = tops;
      console.log(tops);
    }
  }
};
</script>
<style lang='stylus' rel='stylesheet/stylus' scoped>
@import '../../../common/stylus/minxns.styl';

.goods {
  display: flex;
  position: absolute;
  top: 195px;
  bottom: 46px;
  width: 100%;
  background: #fff;
  overflow: hidden;

  .menu-wrapper {
    flex: 0 0 80px;
    width: 80px;
    background: #f3f5f7;

    .menu-item {
      display: table;
      height: 54px;
      width: 56px;
      padding: 0 12px;
      line-height: 14px;

      &.current {
        position: relative;
        z-index: 10;
        margin-top: -1px;
        background: #fff; // 高亮
        color: $green;
        font-weight: 700;

        .text {
          border-none();
        }
      }

      .icon {
        display: inline-block;
        vertical-align: top;
        width: 12px;
        height: 12px;
        margin-right: 2px;
        background-size: 12px 12px;
        background-repeat: no-repeat;
      }

      .text {
        display: table-cell;
        width: 56px;
        vertical-align: middle;
        bottom-border-1px(rgba(7, 17, 27, 0.1));
        font-size: 12px;
      }
    }
  }

  .foods-wrapper {
    flex: 1;

    .title {
      padding-left: 14px;
      height: 26px;
      line-height: 26px;
      border-left: 2px solid #d9dde1;
      font-size: 12px;
      color: rgb(147, 153, 159);
      background: #f3f5f7;
    }

    .food-item {
      display: flex;
      margin: 18px;
      padding-bottom: 18px;
      bottom-border-1px(rgba(7, 17, 27, 0.1));

      &:last-child {
        border-none();
        margin-bottom: 0;
      }

      .icon {
        flex: 0 0 57px;
        margin-right: 10px;
      }

      .content {
        flex: 1;

        .name {
          margin: 2px 0 8px 0;
          height: 14px;
          line-height: 14px;
          font-size: 14px;
          color: rgb(7, 17, 27);
        }

        .desc, .extra {
          line-height: 10px;
          font-size: 10px;
          color: rgb(147, 153, 159);
        }

        .desc {
          line-height: 12px;
          margin-bottom: 8px;
        }

        .extra {
          .count {
            margin-right: 12px;
          }
        }

        .price {
          font-weight: 700;
          line-height: 24px;

          .now {
            margin-right: 8px;
            font-size: 14px;
            color: rgb(240, 20, 20);
          }

          .old {
            text-decoration: line-through;
            font-size: 10px;
            color: rgb(147, 153, 159);
          }
        }

        .cartcontrol-wrapper {
          position: absolute;
          right: 0;
          bottom: 12px;
        }
      }
    }
  }
}
</style>